package com.chainyoung.trademarket.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.TimeUtils;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.entity.Ad;
import com.chainyoung.trademarket.ui.AdDetailActivity;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class InformationAdapter extends BaseQuickAdapter<Ad, BaseViewHolder> {

    private ImageLoader mImageLoader;
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sd1 = new SimpleDateFormat("HH:mm");
    SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");

    public InformationAdapter(@Nullable List<Ad> data) {
        super(R.layout.layout_information_item, data);
        mImageLoader = Utils.getAppComponent().imageLoader();
    }


    @Override
    protected void convert(BaseViewHolder helper, final Ad item) {
        // helper.setText(R.id.tv_from, "Cbob.pro");
        Date transactionTime = new Date(Long.valueOf(item.getAddtime()) * 1000);
        int isYesterday = 1;
        try {
            isYesterday = TimeUtils.isYeaterday(transactionTime, new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.tv_browse_num, isYesterday == -1 ? sd1.format(transactionTime) : isYesterday == 0 ? "昨天" : sd2.format(transactionTime));
        helper.setText(R.id.tv_title, item.getTitle());
        //helper.setText(R.id.tv_browse_num, "2000浏览");
        mImageLoader.loadImage(mContext,
                GlideImageConfig
                        .builder()
                        .url(item.getImg())
                        .errorPic(R.mipmap.ic_default)
                        .imagerView((ImageView) (helper.getView(R.id.iv)))
                        .build());

        helper.getView(R.id.rl_information).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AdDetailActivity.class);
                intent.putExtra(URL, item.getUrl());
                intent.putExtra("title",item.getTitle());
                mContext.startActivity(intent);
            }
        });

    }


}
